var edit_var = {
    heightlight_css: "",//使用的代码高亮css类名
    isEdit: false,//是否处于编辑状态
    artId: "",//处于编辑状态的文章id
    curImgKey: "",
    curZipKey: ""
}
var editor = null;
$(function () {
    $("[data-toggle='popover']").popover();
    loadNavBarTree();//加载菜单信息
    var E = window.wangEditor;
    editor = new E("#wangeditorContent");
    // 表情面板可以有多个 tab ，因此要配置成一个数组。数组每个元素代表一个 tab 的配置
    var em_content = [];
    for (var i = 1; i <= 59; i++) {
        var json = {};
        json.src = 'http://home.zhangbin.xin/static/img/face/comment/' + i + '.gif';
        em_content.push(json)
    }
    editor.customConfig.emotions = [
        {
            title: 'QQ小黄脸',
            type: 'image',
            content: em_content
        }
    ];

    editor.create();
    $(".w-e-text-container").css("height", "530");
    /* $("html,.w-e-text,#wangeditorContainer").niceScroll({
         cursorcolor:"#48c55e",
         cursoropacitymax:1,
         touchbehavior:false,
         cursorwidth:"3px",
         cursorborder:"0",
         cursorborderradius:"3px",
         autohidemode: false,
     });*/
})


function timesClick() {
    $(".modal").fadeOut();
    $(".modal-backdrop").fadeOut();
}

function reUploadBannerBtnClick() {
    $("#myModalLabel").text("重新上传Banner图片");
    $(".modal").fadeIn();
    $(".modal-backdrop").fadeIn();
    $(".upload_banner_file").show();
    $(".upload_zip_file").hide();
}

function reloadDemoFileBtnClick() {
    $("#myModalLabel").text("重新上传演示压缩文件");
    $(".modal").fadeIn();
    $(".modal-backdrop").fadeIn();
    $(".upload_banner_file").hide();
    $(".upload_zip_file").show();
}

function updateArticle(param) {
    param.ecnryptId = edit_var.artId;
    if (!param.ecnryptId) {
        layer.msg("参数错误", fn);
        return false;
    }
    var def = $.ajax({
        url: "/article/back/updateArticle.zb",
        type: "POST",
        daatType: "JSON",
        data: param
    });
    def.done(function (res) {
        if (res && res.success) {
            if (param.status == 1) {
                layer.msg("发布成功")
            } else {
                layer.msg("保存成功")
            }
        } else {
            if (param.status == 1) {
                layer.msg("发布失败", fn)
            } else {
                layer.msg("保存失败", fn)
            }
        }
    });
    def.fail(function () {
        if (param.status == 1) {
            layer.msg("发布失败", fn)
        } else {
            layer.msg("保存失败", fn)
        }
    })
    def.always(function () {
        $(".modal-backdrop").fadeOut();
    })
}

function isDemoClick() {
    if ($(this).attr("data") == 1) {
        $(".demo_url").css("visibility", "visible");
    } else {
        $(".demo_url").css("visibility", "hidden");
    }
}

function submitClick() {
    var param = validateParam();
    if ($.isPlainObject(param)) {
        $(".modal-backdrop").fadeIn();
        param.blog_navbar_id = $("#menu").data("id");
        var dataVal = $(this).attr("data-val");
        if (dataVal === "saveAndPublish") {
            param.status = 1;
        } else {
            param.status = 0;
        }
        if (edit_var.heightlight_css) {
            param.hightlight_css = edit_var.heightlight_css;
        } else {
            param.hightlight_css = "default";
        }
        if (edit_var.isEdit) {
            updateArticle(param);
        } else {
            addArticle(param);
        }
    }
}

function addArticle(param) {
    var def = $.ajax({
        url: "/article/back/addArticle.zb",
        type: "POST",
        daatType: "JSON",
        data: param
    });
    def.done(function (res) {
        if (res && res.success) {
            if (param.status == 1) {
                layer.msg("发布成功,即将刷新数据！", function () {
                    $(".modal-backdrop").fadeOut();
                    window.location.reload()
                })
            } else {
                layer.msg("保存成功,即将刷新数据！", function () {
                    $(".modal-backdrop").fadeOut();
                    window.location.reload()
                })
            }
        } else {
            if (param.status == 1) {
                layer.msg("发布失败", fn)
            } else {
                layer.msg("保存失败", fn)
            }
        }
    });
    def.fail(function () {
        if (param.status == 1) {
            layer.msg("发布失败", fn)
        } else {
            layer.msg("保存失败", fn)
        }
    })
}

function validateParam() {
    var passTem = true;
    var param = {};
    param.title = $("#title").val().trim();
    param.menu = $("#menu").val().trim();
    param.front_descr = $("#front_descr").val().trim();
    param.front_img = $("#front_img").val().trim();
    param.back_descr = $("#back_descr").val().trim();
    param.is_demo = $("#is_demo").val().trim();
    param.is_download = $("#is_download").val().trim();

    for (var key in param) {
        if (!param[key]) {
            $("#" + key).attr("data-content", "此字段不能为空").popover('show');
            passTem = false;
        } else {
            $("#" + key).popover('hide');//这里主要是为了给#menu的popover隐藏
        }
    }
    var wangeditorContentText = editor.txt.text();
    if (!wangeditorContentText) {
        $("#wangeditorContent").parent().attr("data-content", "文本不能为空").popover('show');
        passTem = false;
    } else {
        param.content = editor.txt.html();
    }
    if (!param.front_img) {
        $(".upload_banner_file").attr("data-content", "请上传翻牌banner").popover('show');
    }
    var demoUrl = $("#demo_url").val().trim();
    if ($("#is_demo").attr("data") == 1) {
        if (!demoUrl) {
            $("#demo_url").attr("data-content", "请输入演示地址").popover('show');
            passTem = false;
        } else {
            param.demo_url = demoUrl;
            $("#demo_url").popover('hide');
        }
    } else {
        $("#demo_url").popover('hide');
    }
    var zip = $("#is_download").attr("data-zip");
    if ($("#is_download").attr("data") == 1) {
        if (!zip) {
            $(".upload_zip_file").attr("data-content", "请上传资源文件").popover('show');
            passTem = false;
        } else {
            param.zip_url = zip;
            $(".upload_zip_file").popover('hide')
        }
    } else {//即使不可以下载资源，但是也可以添加资源
        if (zip) {
            param.zip_url = zip;
        }
    }
    if (passTem) {
        param.is_demo = $("#is_demo").attr("data");
        param.is_download = $("#is_download").attr("data");
        param.demo_url = demoUrl;
        return param;
    } else {
        return false;
    }
}

function inputKeyUpEvent() {
    if ($(this).val()) {
        $(this).popover('hide');
    } else {
        $(this).attr("data-content", "此字段不能为空").popover('show');
    }
}

function getUrlParm(param) {
    var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function fn() {
};